/* Created by jankoatwarpspeed.com */

(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: "" 
        }, options); 
        
        var element = this;
        var group = -1;
        var groupLookup = [];

        var oldstep = "";
        var steps = $(element).find("fieldset");
        //var counter = $(element).find("div.wizard-counter");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();
        
        // 2
        $(element).before("<ul id='steps'></ul>");

        steps.each(function(i) {
            
            $("div.wizard-counter-"+i).text('Question '+(i+1)+' out of '+count);
            $(this).wrap("<div id='step" + i + "'></div>");
            $(this).append("<p id='step" + i + "commands'></p>");

            // 2
            var name = $(this).find("legend").html();
            if(oldstep!=name) {
              $("#steps").append("<li id='stepDesc" + i + "'><span>" + name + "</span></li>");
              group = i;
            }
            groupLookup[i] = group;
            //console.log(i+'->'+group);

            if (i == 0) {
                createSubmitButton(i);
                createPrevButtonDisabled(i);
                createNextButton(i);
                selectStep(groupLookup[i]);
            }
            else if (i == count - 1) {
                $("#step" + i).hide();
                createSubmitButton(i)
                createPrevButton(i);
                createNextButtonDisabled(i);
            }
            else {
                $("#step" + i).hide();
                createSubmitButton(i)
                createPrevButton(i);
                createNextButton(i);
            }
            oldstep=name;
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Prev' class='prev'>&laquo; Back</a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                selectStep(groupLookup[(i - 1)]);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Next &raquo;</a>");

            $("#" + stepName + "Next").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(groupLookup[(i + 1)]);
            });
        }

        function createNextButtonDisabled(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<p class='nextDisabled'>Next &raquo;</p>");
        }
        
        function createPrevButtonDisabled(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<p class='prevDisabled'>&laquo; Back</p>");
        }        


        function createSubmitButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<input class='wizard-save' type='submit' value='Save' />");
        }
        

        function selectStep(i) {
            //alert(i);
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }
        
        
    }
})(jQuery); 